//
//  NSDate+DateFormat.m
//  ELearningClient
//
//  Created by Jim Fu on 2018/12/19.
//

#import "NSDate+DateFormat.h"
#import "MKCDateFormatterEngine.h"
#import <mach/mach_time.h>
#import <sys/sysctl.h>

@implementation NSDate (DateFormat)

- (NSString *)convertToFormattedStringWithoutTime {
	NSDate *dateNow = [NSDate date];
	NSTimeInterval timeInterval = [dateNow timeIntervalSinceDate:self];
	
	NSString *dateString = @"";
	if (timeInterval < DATE_MINUTE) {
		dateString = [NSString stringWithFormat:@"1%@",NSLocalizedString(@"分钟内", nil)];
	}
	else if (timeInterval < DATE_HOUR) {
		NSTimeInterval minutes = timeInterval / DATE_MINUTE;
		dateString = [NSString stringWithFormat:@"%d%@",(int)minutes,NSLocalizedString(@"分钟前", nil)];
	}
	else if (timeInterval < DATE_DAY) {
		NSTimeInterval hours = timeInterval / DATE_HOUR;
		dateString = [NSString stringWithFormat:@"%d%@",(int)hours,NSLocalizedString(@"小时前", nil)];
	}
	else {
        NSDateFormatter *formatter = [MKCDateFormatterEngine sharedEngine].yyyyDateFormatter;
		
		NSString *yearNowString = [formatter stringFromDate:dateNow];
		NSString *yearString = [formatter stringFromDate:self];
		if ([yearString isEqualToString:yearNowString]) {
            formatter = [MKCDateFormatterEngine sharedEngine].mmddDateFormatter;
        }
		else {
            formatter = [MKCDateFormatterEngine sharedEngine].yyyymmddDateFormatter;
		}
		
		dateString = [formatter stringFromDate:self];
	}
	return dateString;
}

- (NSString *)convertToFormattedString {
    
   return  [self coverDateFormat:NO];
}
- (NSString *)converToDayDate{
    return [self coverDateFormat:YES];
}
- (NSString *)coverDateFormat:(BOOL)isShowYMD{
    
    NSDate *dateNow = [NSDate date];
    NSTimeInterval timeInterval = [dateNow timeIntervalSinceDate:self];
    
    NSString *dateString = @"";
    if (timeInterval < DATE_MINUTE) {
        dateString = [NSString stringWithFormat:@"1%@",NSLocalizedString(@"分钟内", nil)];
    }
    else if (timeInterval < DATE_HOUR) {
        NSTimeInterval minutes = timeInterval / DATE_MINUTE;
        dateString = [NSString stringWithFormat:@"%d%@",(int)minutes,NSLocalizedString(@"分钟前", nil)];
    }
    else if (timeInterval < DATE_DAY) {
        NSTimeInterval hours = timeInterval / DATE_HOUR;
        dateString = [NSString stringWithFormat:@"%d%@",(int)hours,NSLocalizedString(@"小时前", nil)];
    }
    else {
        if (isShowYMD) {
            
           NSDateFormatter *formatter = [MKCDateFormatterEngine sharedEngine].yyyymmddDateFormatter;
            dateString = [formatter stringFromDate:self];
            
        }else{
            NSDateFormatter *formatter = [MKCDateFormatterEngine sharedEngine].yyyyDateFormatter;
            
            NSString *yearNowString = [formatter stringFromDate:dateNow];
            NSString *yearString = [formatter stringFromDate:self];
            if ([yearString isEqualToString:yearNowString]) {
                formatter = [MKCDateFormatterEngine sharedEngine].mmddhhmmDateFormatter;
            }
            else {
                formatter = [MKCDateFormatterEngine sharedEngine].yyyymmddhhmmDateFormatter;
            }
            
            dateString = [formatter stringFromDate:self];
        }
        
    }
    return dateString;
}

- (NSString *)convertToStandardFormatString {
    NSDateFormatter *formatter = [MKCDateFormatterEngine sharedEngine].yyyymmdd_hhmmssDateFormatter;
    
    NSString *dateString = [formatter stringFromDate:self];
    return dateString;
}

- (NSString *)convertToStandardFormatStringWithYMDHMS {
    NSDateFormatter *formatter = [MKCDateFormatterEngine sharedEngine].yyyymmddhhmmssDateFormatter;
    
    NSString *dateString = [formatter stringFromDate:self];
    return dateString;
}

- (NSString *)convertToDateFormatString {
    NSDateFormatter *formatter = [MKCDateFormatterEngine sharedEngine].yyyymmddDateFormatter;
    
    NSString *dateString = [formatter stringFromDate:self];
    return dateString;
}
- (NSString *)convertToDateFormatPointString{
    NSDateFormatter *formatter = [MKCDateFormatterEngine sharedEngine].yyyymmddDatePointFormatter;
    
    NSString *dateString = [formatter stringFromDate:self];
    return dateString;
}

- (NSString *)convertToYearMonthDayDateFormatString {
	NSDateFormatter *formatter = [MKCDateFormatterEngine sharedEngine].yyyymmddChineseDateFormatter;
	
	NSString *dateString = [formatter stringFromDate:self];
	return dateString;
}

- (NSString *)convertToMonthDayDateFormatString{
    NSDateFormatter *formatter = [MKCDateFormatterEngine sharedEngine].mmddhhmmDateFormatter;

    NSString *dateString = [formatter stringFromDate:self];
    return dateString;
}

- (NSString *)convertToLongDateFormatString {
	NSDateFormatter *formatter = [MKCDateFormatterEngine sharedEngine].yyyymmddhhmmssChineseDateFormatter;
	
	NSString *dateString = [formatter stringFromDate:self];
	return dateString;
}

- (NSString *)coverToDataWithyyyymmddhhmm{

    NSDateFormatter *formatter = [MKCDateFormatterEngine sharedEngine].yyyymmddhhmmDateFormatter;

    NSString *dateString = [formatter stringFromDate:self];
    return dateString;

}

/**
 *  Convert NSDate to yyyy/MM/dd
 *
 *  @return formatted string
 */
- (NSString *)coverToDateWithyyyymmdd{

    NSDateFormatter *formatter = [MKCDateFormatterEngine sharedEngine].yyyymmddDateLineFormatter;

    NSString *dateString = [formatter stringFromDate:self];
    return dateString;

}

+ (instancetype)zeroToday {
    NSDate *now = [NSDate date];
    return [NSDate zeroWithDate:now];
}

+ (instancetype)zeroWithDate:(NSDate *)date {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay
                                               fromDate:date];
    NSDate *zeroDate = [calendar dateFromComponents:components];
    return zeroDate;
}

- (NSString *)convertToWeekdayString
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    if ([calendar isDateInToday:self]) {
        return @"今天";
    }
    else if ([calendar isDateInYesterday:self])
    {
        return @"昨天";
    }
    else
    {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setTimeStyle:NSDateFormatterMediumStyle];
        
        //int week=0;
        NSDateComponents *comps = [calendar components:NSCalendarUnitWeekday fromDate:self];
        NSInteger week = [comps weekday];
        NSArray *weeks = @[@"周日",@"周一",@"周二",@"周三",@"周四",@"周五",@"周六"];
        
        return [weeks objectAtIndex:week-1];
    }
}

+ (NSString *)convertSecondToString:(NSTimeInterval)time {
    NSString *string = @"";
    time = round(time);
    int minute = time / 60;
    int second = (int)time % 60;
    if (minute == 0) {
        string = [NSString stringWithFormat:@"%d″",second];
    }
    else {
        string = [NSString stringWithFormat:@"%d′%d″",minute,second];
    }
    return string;
}

- (NSString *)convertDaysSineNow {
    NSTimeInterval time = self.timeIntervalSinceNow;
    if (time > 0) {
        NSInteger day = time / DATE_DAY;
        return [NSString stringWithFormat:@"%ld",(long)day];
    }
    else {
        return nil;
    }
}

+ (NSDate *)convertLocaleDateWithZeroTime:(NSDate *)utcDate {
//    NSTimeZone *zone = [NSTimeZone systemTimeZone];
//    NSInteger interval = [zone secondsFromGMTForDate: utcDate];
//    NSDate *localeDate = [utcDate dateByAddingTimeInterval: interval];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSUIntegerMax fromDate:utcDate];
    components.hour = 0;
    components.minute = 0;
    components.second = 0;
    NSTimeInterval ts = (double)(int)[[calendar dateFromComponents:components] timeIntervalSince1970];
    return [NSDate dateWithTimeIntervalSince1970:ts];
}

+ (double)getCPUTime
{
    mach_timebase_info_data_t info;
    if (mach_timebase_info(&info) != KERN_SUCCESS) return -1.0f;
    uint64_t elapsed = mach_absolute_time();
    
    uint64_t nanos = elapsed * info.numer / info.denom;
    return (double)nanos / NSEC_PER_SEC;
    
}

+ (time_t)uptime
{
    
    struct timeval boottime;
    int mib[2] = {CTL_KERN, KERN_BOOTTIME};
    size_t size = sizeof(boottime);
    time_t now;
    time_t uptime = -1;
    (void)time(&now);
    if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 && boottime.tv_sec != 0) {
        uptime = now - boottime.tv_sec;
    }
    return uptime;
}

@end
